/**
 * Copyright 2010 Butin Kirill <kiryaka@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.flexupload.remote
{
	import flash.events.Event;
	import flash.utils.setTimeout;
	
	import mx.rpc.events.ResultEvent;
	
	import org.flexupload.model.FileInfo;
	import org.flexupload.model.FilePart;
	import org.flexupload.model.ServerReply;

	public dynamic class ServerSimulationService extends FlexuploadService
	{
		private var reply:ServerReply = new ServerReply();
		public static var bytes:uint = 0;
		public static var bytesLastTime:uint = 0;
		public static var timeOut:uint = 300;
		public function ServerSimulationService(destination:String = null){
			reply.status = ServerReply.SUCCESS;
		}
		
		public function isServerFree():void{
			var r:ServerReply = new ServerReply();
			r.status = ServerReply.SUCCESS;
			r.type = -1;
			var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, r);
			//dispatchEvent(event);
			setTimeout(dispatchEvent, 1500, event);
		}		
		public function startUpload(fileInfo:FileInfo):void{
			fileInfo.tempFileName = fileInfo.name;
			reply.type = ServerReply.TYPE_START_UPLOAD;
			reply.fileInfo = fileInfo;
			var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, reply);
			//trace("--Send start: "+reply.fileInfo);
			dispatchEvent(event);
			//setTimeout(dispatchEvent, 10, event);
		}
		public function uploadFilePart(fileInfo:FileInfo, filePart:FilePart):void{
			reply.type = ServerReply.TYPE_SAVE_PART;
			reply.fileInfo = fileInfo;
			reply.bytesTransfered = filePart.length;
			ServerSimulationService.bytes += filePart.length;
			var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, reply);
			var time:uint = (filePart.length * strategy.getActiveServicesCount()) / 600 + 1;
			//FlexGlobals.topLevelApplication.simple.debug.text = strategy.getActiveServicesCount();
			setTimeout(dispatchEvent, time, event);
		}
		public function finishUpload(fileInfo:FileInfo):void{
			reply.type = ServerReply.TYPE_FINISH_UPLOAD;
			reply.fileInfo = fileInfo;
			var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, reply);
			//trace("--Send end: "+reply.fileInfo);
			dispatchEvent(event);
			//setTimeout(dispatchEvent, 10, event);
		}
		public function finishSession():void{
			reply.type = ServerReply.TYPE_FINISH_SESSION;
			var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, reply);
			//trace("--Send end of session: " + reply.type);
			dispatchEvent(event);
			//setTimeout(dispatchEvent, 10, event);
		}		
		override public function dispatchEvent(event:Event):Boolean
		{
			if (ResultEvent(event).result.type == ServerReply.TYPE_FINISH_UPLOAD  || ResultEvent(event).result.type == ServerReply.TYPE_START_UPLOAD){
				//trace("--dispatch event on: " + ResultEvent(event).result.fileInfo);
			}	
			return super.dispatchEvent(event);
		}
	}
}